<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\ResponseException;
use App\Http\Requests\Api\Article\IndexRequest;
use App\Models\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
	/**
	 * 文章列表
	 * User: Clannad ~ ☆
	 * DateTime: 2019/11/2 15:07
	 * @param IndexRequest $request
	 * @throws ResponseException
	 */
	public function index(IndexRequest $request)
	{
		$category_id = $request->input('category');
		$pageSize    = $request->input('size') ?? 10;
		$title       = $request->input('title');
		$author      = $request->input('author');
		$orderBy     = $request->input('order') ?? null;
		$sort        = $request->input('sort') ?? 'desc';
		$start_time  = $request->input('start_time');
		$end_time    = $request->input('end_time');

		$articleList = Article::query()
			->with(['category'])
			->select(['id','title','author','thumb','content','is_show','sort','created_at','category_id'])
			->when($title, function ($query) use ($title){
				return $query->where('title', 'like', '%' . $title . '%');
			})
			->when($author, function ($query) use ($author){
				return $query->where('author', 'like', '%' . $author . '%');
			})
			->when($category_id, function ($query) use ($category_id){
				return $query->where('category_id', '=', $category_id);
			})
			->when($start_time,function($query) use ($start_time){
				return $query->where('created_at','>=',$start_time);
			})
			->when($end_time,function($query) use ($end_time){
				return $query->where('created_at','<',$end_time);
			})
			->when($orderBy, function ($query) use ($orderBy, $sort){
				return $query->orderBy($orderBy, $sort);
			}, function ($query) use ($sort){
				return $query->orderBy('sort', $sort);
			})
			->paginate($pageSize);


		if ($articleList->count()) {

			foreach ($articleList as $k => $v){
				$v['category_name'] = $v->category->category_name;
				unset($v['category']);
				$articleList[$k] = $v;
			}

			throw new ResponseException(200, 'ok', $articleList);
		} else {
			throw new ResponseException(404, '没有文章数据',);
		}
	}

	/**
	 * 文章显示
	 * User: Clannad ~ ☆
	 * DateTime: 2019/11/2 09:05
	 * @param $id
	 */
	public function show($id)
	{
		$article = Article::with(['category'])
			->select(['title','thumb','author','category_id','content','created_at','id'])
			->where('is_show',1)
			->find($id);

		if(!$article){
		 	throw new ResponseException(404,'文章未找到');
		}

		//文章标签
		$article_tag = $article->tag()
					->select('tag_id','tag_name')
					->get()
					->map(function ($tag){
						return [
							'id'=>$tag->tag_id,
							'tag_name'=>$tag->tag_name,
						];
					});

		//处理下通过预加载获取出来的数据
		$article['category_name'] = $article['category']['category_name'];
		unset($article['category']);

		//文章评论
		$comment = $article->comment()
					->select([
						'comment.id','reply_user_id','pid','content','comment.created_at','user.nick_name as comment_user',
						'u.nick_name as to_user'
					])
					->where('comment.is_show',1)
					->leftJoin('user','comment.user_id','=','user.id')
					->leftJoin('user as u','comment.reply_user_id','=','u.id')
					->with(['comment_reply'=>function($query){
						return $query
								->select(['reply_content','comment_reply.created_at','comment_id','nickname'])
								->LeftJoin('admin','comment_reply.admin_id','=','admin.id')
								->get();
					}])
					->get()
					->toArray();

		$commentList = [];
		foreach ($comment as $k => $v) {
		    if($v['pid'] == '0'){
				foreach ($comment as $k1 => $v1) {
				    if($v1['pid'] == $v['id']){
				    	$v['child'][] = $v1;
				    }
				}
				$commentList[] = $v;
		    }
		}

		$data = [
			'article'=>$article,
			'article_tag'=>$article_tag,
			'commentList'=>$commentList,
		];

		throw new ResponseException(200,'ok',$data);
	}


}
